package com.wecan.data.mybatis;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

/**
 * @Author liuzhihao
 * @Date 2022.01.11
 * @Version 1.0.0
 * @Description 字段自动填充
 */
@Slf4j
public class BatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, "createDate", System.currentTimeMillis());
        this.fillStrategy(metaObject, "updateDate", System.currentTimeMillis());
        try {
            this.fillStrategy(metaObject, "createUser", StpUtil.getLoginIdAsLong());
        } catch (NotLoginException e) {
            log.info("[APP] |- 未登录无法获取createUser");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, "updateDate", System.currentTimeMillis());
        try {
            this.fillStrategy(metaObject, "updateUser", StpUtil.getLoginIdAsLong());
        } catch (Exception e) {
            log.info("[APP] |- 未登录无法获取createUser");
        }
    }

}
